package com.ruoyi.business.service.impl;

import java.util.List;
import com.ruoyi.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.business.mapper.MotorPartsMapper;
import com.ruoyi.business.domain.MotorParts;
import com.ruoyi.business.service.IMotorPartsService;
import com.ruoyi.common.annotation.DataScope;
import com.ruoyi.common.utils.SecurityUtils;

/**
 * 配件查询Service业务层处理
 *
 * @author ruoyi
 * @date 2025-03-19
 */
@Service
public class MotorPartsServiceImpl implements IMotorPartsService
{
    @Autowired
    private MotorPartsMapper motorPartsMapper;

    /**
     * 查询配件查询
     *
     * @param id 配件查询主键
     * @return 配件查询
     */
    @Override
    public MotorParts selectMotorPartsById(Long id)
    {
        return motorPartsMapper.selectMotorPartsById(id);
    }

    /**
     * 查询配件查询列表
     *
     * @param motorParts 配件查询
     * @return 配件查询
     */
    @Override
    @DataScope(userAlias = "mp") // 添加数据权限注解，指定用户别名为mp
    public List<MotorParts> selectMotorPartsList(MotorParts motorParts)
    {
        return motorPartsMapper.selectMotorPartsList(motorParts);
    }

    /**
     * 新增配件查询
     *
     * @param motorParts 配件查询
     * @return 结果
     */
    @Override
    public int insertMotorParts(MotorParts motorParts)
    {
        motorParts.setCreateTime(DateUtils.getNowDate());
        // 设置创建者为当前登录用户
        motorParts.setCreateBy(SecurityUtils.getUsername());
        
        // 如果编号为空，则自动生成编号
        if (motorParts.getCode() == null || motorParts.getCode().isEmpty()) {
            // 生成日期前缀，格式：PART-yyyyMMdd-
            String datePrefix = "PART-" + DateUtils.dateTimeNow("yyyyMMdd") + "-";
            
            // 查询当天最大编号
            MotorParts queryParams = new MotorParts();
            queryParams.setCode(datePrefix);
            List<MotorParts> list = motorPartsMapper.selectMotorPartsList(queryParams);
            
            int maxSequence = 0;
            for (MotorParts item : list) {
                String code = item.getCode();
                if (code != null && code.startsWith(datePrefix)) {
                    try {
                        String sequenceStr = code.substring(datePrefix.length());
                        int sequence = Integer.parseInt(sequenceStr);
                        if (sequence > maxSequence) {
                            maxSequence = sequence;
                        }
                    } catch (Exception e) {
                        // 忽略解析异常
                    }
                }
            }
            
            // 生成新编号
            String newCode = datePrefix + String.format("%04d", maxSequence + 1);
            motorParts.setCode(newCode);
        }
        
        return motorPartsMapper.insertMotorParts(motorParts);
    }

    /**
     * 修改配件查询
     *
     * @param motorParts 配件查询
     * @return 结果
     */
    @Override
    public int updateMotorParts(MotorParts motorParts)
    {
        motorParts.setUpdateTime(DateUtils.getNowDate());
        // 设置更新者为当前登录用户
        motorParts.setUpdateBy(SecurityUtils.getUsername());
        return motorPartsMapper.updateMotorParts(motorParts);
    }

    /**
     * 批量删除配件查询
     *
     * @param ids 需要删除的配件查询主键
     * @return 结果
     */
    @Override
    public int deleteMotorPartsByIds(Long[] ids)
    {
        return motorPartsMapper.deleteMotorPartsByIds(ids);
    }

    /**
     * 删除配件查询信息
     *
     * @param id 配件查询主键
     * @return 结果
     */
    @Override
    public int deleteMotorPartsById(Long id)
    {
        return motorPartsMapper.deleteMotorPartsById(id);
    }
}
